<? 
	if(isset($_POST['btngo']))
	{
		require("required/dbconnect.php");
		$main_cat=$_POST['main_cat'];
		$sub_cat=$_POST['sub_cat'];
		$pageno=$_POST['pageno'];
		header("Location:product_viewall.php?page=$pageno&main_cat=$main_cat&sub_cat=$sub_cat");
	}
	
	$title="View all products"; 
	$pageid="1";
	$accessid="a00";
	

	require("required/dbconnect.php"); 
	require("required/function.php");
	require("required/header.php");
	
	
	if(empty($_GET['page']))
	{
		$page="1";
	}
	else
	{
		$page=$_GET['page'];
	}
	
	if(isset($_POST['btnsearch']))
	{
		$main_cat=$_POST['main_cat'];
		$main_cat_slt="";
		$sub_cat=$_POST['sub_cat'];
		$subcatslt="";
		
		if($main_cat=="")
		{
			$main_cat_q="";
		}
		else
		{
			$main_cat_q="and cat_id='$main_cat' ";
		}
		
		if($sub_cat=="" or $sub_cat=="-1")
		{
			$sub_cat_q="";
		}
		else
		{
			$sub_cat_q="and sub_cat_id='$sub_cat' ";
		}
	}
	else
	{
		if(empty($_GET['main_cat']))
		{
			$main_cat="";
			$main_cat_slt="selected";
			$main_cat_q="";
		}
		else
		{
			$main_cat=$_GET['main_cat'];
			$main_cat_slt="";
			$main_cat_q="and cat_id='$main_cat' ";
		}
		
		if(empty($_GET['sub_cat']))
		{
			$sub_cat="";
			$sub_cat_slt="selected";
			$sub_cat_q="";
		}
		else
		{
			$sub_cat=$_GET['sub_cat'];
			$sub_cat_slt="";
			if($sub_cat!='-1')
			{
				$sub_cat_q="and sub_cat_id='$sub_cat' ";
			}
			else
			{
				$sub_cat_q="";
			}
		}
		
	
	}
		
		$totalperpage=30;

	$from=($page-1)*$totalperpage;	
	
	
	$get_products=mysql_query("select * from os_products where  cat_id is not null $main_cat_q $sub_cat_q order by cat_id,product_name_eng asc limit $from,$totalperpage")or die(mysql_error());
	$get_total=mysql_query("select * from os_products where cat_id is not null $main_cat_q $sub_cat_q")or die(mysql_error());
	
?>
<table width="100%">

<?
	$count=mysql_num_rows($get_total);

	$totalpage=$count/$totalperpage;
	
	if($count%$totalperpage>0)
	{
		if(round($totalpage)>$totalpage)
		{
			$totalpage=round($totalpage);
		}
		else
		{
			$totalpage=round($totalpage+1);
		}
	}
	else
	{
		$totalpage=round($totalpage);
	}
	
	
	$nextpage=$page+1;
	$prepage=$page-1;
	
	
?>
<td align="left" valign="top">
	<strong>View All Products</strong>
	<table width="100%" cellpadding="2" cellspacing="1" class="solidborder" >
		<? if(mysql_num_rows($get_products)==0){ ?>
		<tr>
		 <td height="200" valign="middle" colspan="8" align="center">
			<font color="#FF0000">- No product found -</font><br>
			<a href="product_add.php">Add product</a>
		 </td>
		</tr>
		<? }else{ ?>
		<tr>
					<td width="100%" colspan="10" align="left"><br><br>
						<table width="100%" cellpadding="0" cellspacing="0" >
							<td width="47%" height="45">
							<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
							<input type="button" class="button" value="<<" onClick="window.open('product_viewall.php?page=1','_self')">
							<input type="button" class="button" value="<" onClick="window.open('product_viewall.php?page=<?= $prepage ?>&main_cat=<?= $main_cat ?>&sub_cat=<?= $sub_cat ?>','_self')">
							
							Page <input type="text" name="pageno" value="<?= $page ?>" size="2" maxlength="3" class="text"> of <?= $totalpage ?>
							<input type="submit" value="Go" class="button" name="btngo">
							<? if($page==$totalpage){ ?>	
							<input type="button" class="button" value=">" >
							<? }else{ ?>
							<input type="button" class="button" value=">" onClick="window.open('product_viewall.php?page=<?= $nextpage ?>&main_cat=<?= $main_cat ?>&sub_cat=<?= $sub_cat ?>','_self')">
							<? } ?>
							<input type="button" class="button" value=">>" onClick="window.open('product_viewall.php?page=<?= $totalpage ?>&main_cat=<?= $main_cat ?>&sub_cat=<?= $sub_cat ?>','_self')">
							
							</form>
							
							</td>
						<td width="53%"  align="left" valign="top">
						<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">	
						<table width="100%">
						<script language="JavaScript" type="text/javascript" src="required/js/select_cat.js"></script>
						<?
							$get_cat=mysql_query("select * from os_category order by id asc");
						
						?>
						<Td width="20%">View Category</Td>
							<td width="19%">
								<select name="main_cat" class="text" onChange="showSubCat(this.value)">
									<option value="" <?= $main_cat_slt ?>>All Category</option>
								<?
									while($c=mysql_fetch_array($get_cat,MYSQL_ASSOC))
									{
										if($c['id']==$main_cat)
										{
											$cslt="selected";
										}
										else
										{
											$cslt="";
										}
								?>
									<option value="<?= $c['id'] ?>" <?= $cslt ?>><?= $c['name_chn'] ?> <?= $c['name_eng'] ?></option>
								<? } ?>
								</select>
								
							</td>
						
							<td width="37%">
							<div id="divResult">
								<?
									if($main_cat!="")
									{
										$get_subcat=mysql_query("select * from os_sub_category where main_category=".$main_cat." order by id desc");
										if($sub_cat=='-1')
										{
											$soption="<option value='-1' selected>All Sub Category</option>";
											$soption2="";
										}
										else
										{
											$soption="<option value=''>Please select</option>";
											$soption2="<option value='-1'>All</option>";
										}
								?>
										<select name="sub_cat" class="text">
										<?= $soption ?>
										<? 
											while($s=mysql_fetch_array($get_subcat,MYSQL_ASSOC))
											{
												if($s['id']==$sub_cat)
												{
													$sslt="selected";
												}
												else
												{
													$sslt="";
												}	
										?>
												<option <?= $sslt ?> value="<?= $s['id'] ?>"><?= $s['name_chn'] ?> <?= $s['name_eng'] ?></option>
										<? 
											}
										?>
										<?= $soption2 ?>
										</select>
								<?	
									}else{
								?>				
								
								<select name="sub_cat" class="text">
									<option value="">All Sub Category</option>
								</select>		
								<? } ?>
							</div>
							</td>
							<td width="24%"><input type="submit" value="View" class="button" name="btnsearch"></td>
						</table>
						</form>
					</td>
					
					</table>
					</td>
		</tr>
		<tr height="20" align="center" style="background: url(images/tbl_th.png);color:#FFFFFF" >
			<Td width="6%">No</Td><td width="35%">Product name</td><td width="18%">Category</td><td>Sub Category</td><td width="19%">create time</td><td width="7%">Action</td>
		</tr>
		<?
			$no=$from+1;
			while($a=mysql_fetch_array($get_products))
			{
				if($no%2==0)
				{
					$bgcolor="#e5e5e5";
				}
				else
				{
					$bgcolor="#eeeeee";	
					
				}
				
				$get_cat=mysql_query("select * from os_category where id=".$a['cat_id'])or die(mysql_error());
				if(mysql_num_rows($get_cat)==0)
				{
					$cat="";
				}
				else
				{
					$cat=mysql_result($get_cat,0,"name_chn")." ".mysql_result($get_cat,0,"name_eng");
				}
				
				$get_subcat=mysql_query("select * from os_sub_category where id=".$a['sub_cat_id'])or die(mysql_error());
				if(mysql_num_rows($get_subcat)==0)
				{
					$subcat="";
				}
				else
				{
					$subcat=mysql_result($get_subcat,0,"name_chn")." ".mysql_result($get_subcat,0,"name_eng");
				}
				
				/*
				if($a['status']==0)
				{
					$status="<font color=red>No</font>";
				}
				else
				{
					$status="<font color=green>Yes</font>";
				}
				*/
				
				
		?>	
		<tr height="20" bgcolor="<?= $bgcolor ?>" onmouseover="this.style.backgroundColor='ccccff'" onmouseout="this.style.backgroundColor='<?= $bgcolor ?>'">
			<td><?= $no ?></td><Td><?= $a['product_name_chn'] ?> <?= $a['product_name_eng'] ?></Td><td><?= $cat ?></td><td><?= $subcat ?></td><td align="center"><?= $a['create_datetime'] ?></td><td><a href="product_viewfull.php?id=<?= $a['id'] ?>">View</a></td>
		</Tr>
		<? $no++;}} ?>
	</table>
	</td>
</table>

<? require("required/footer.php"); ?>

